package com.zjhcsoft.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjhcsoft.auth.common.enums.StatusCode;
import com.zjhcsoft.auth.common.vo.Result;
import com.zjhcsoft.auth.dao.PasswordPolicyMapper;
import com.zjhcsoft.auth.entity.AppResource;
import com.zjhcsoft.auth.entity.Password;
import com.zjhcsoft.auth.entity.PasswordPolicy;
import com.zjhcsoft.auth.entity.web.PasswordPolicyRequest;
import com.zjhcsoft.auth.service.AppResourceService;
import com.zjhcsoft.auth.service.PasswordPolicyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 密码策略表 服务类
 * </p>
 *
 * @author daiwc
 * @since 2021-04-4
 */
@Service
public class PasswordPolicyServiceImpl extends ServiceImpl<PasswordPolicyMapper, PasswordPolicy> implements
        PasswordPolicyService {
    @Autowired
    private AppResourceService appResourceService;
    @Override
    public Result passwordChcek(PasswordPolicyRequest request) {
        String resultMsg = "校验成功";
        String errorMesultMsg = "";
        AppResource app = appResourceService.getAppResourceByChannelCodeAndCorporationCode(request.getChannelCode(),request.getCorporationCode());
        if(app == null){
            return Result.error("应用系统不存在");
        }
        //获取校验规则
        List<PasswordPolicy> policyList = this.lambdaQuery()
                .eq(PasswordPolicy::getStatusCd, "0")
                .eq(PasswordPolicy::getAppId, app.getId()).list();

        String password = request.getPassword();
        for(PasswordPolicy passwordPolicy : policyList){
            String result = validate(passwordPolicy,password);
            if (result != null) {
                errorMesultMsg += result;
            }
        }
        if(StringUtils.isNotEmpty(errorMesultMsg)){
            return Result.error(errorMesultMsg);
        }
        return Result.ok(resultMsg);
    }


    private String validate(PasswordPolicy passwordPolicy, String password) {

        if (StringUtils.isEmpty(passwordPolicy.getVal())) {
            return null;
        }
        String express = MessageFormat.format(passwordPolicy.getExpression(),passwordPolicy.getVal());

        Pattern p = Pattern.compile(express);
        Matcher m = p.matcher(password);
        int i = 0;
        while (m.find()) {
            i++;
        }
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(i + " " + passwordPolicy.getOperator() + " "
                + passwordPolicy.getVal());
        if (exp.getValue(Boolean.class)) {
            return formatMessage(passwordPolicy.getMessage(),
                    passwordPolicy.getVal(),passwordPolicy.getUnit());
        }
        return null;
    }
    protected String formatMessage(String pattern, Object... arguments) {
        return MessageFormat.format(pattern, arguments);
    }

}
